{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 对全体数据，随机选择其中80%做训练数据，剩下20%为测试数据，评价指标为RMSE。（10分）\n",
    "# 4. 用训练数据训练最小二乘线性回归模型（20分）、岭回归模型、Lasso模型，其中岭回归模型（30分）和Lasso模型（30分），注意岭回归模型和Lasso模型的正则超参数调优。\n",
    "# 5. 比较用上述三种模型得到的各特征的系数，以及各模型在测试集上的性能。并简单说明原因。（10分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>dteday</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>casual</th>\n",
       "      <th>registered</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2011-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.344167</td>\n",
       "      <td>0.363625</td>\n",
       "      <td>0.805833</td>\n",
       "      <td>0.160446</td>\n",
       "      <td>331</td>\n",
       "      <td>654</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2011-01-02</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.363478</td>\n",
       "      <td>0.353739</td>\n",
       "      <td>0.696087</td>\n",
       "      <td>0.248539</td>\n",
       "      <td>131</td>\n",
       "      <td>670</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2011-01-03</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.196364</td>\n",
       "      <td>0.189405</td>\n",
       "      <td>0.437273</td>\n",
       "      <td>0.248309</td>\n",
       "      <td>120</td>\n",
       "      <td>1229</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2011-01-04</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.212122</td>\n",
       "      <td>0.590435</td>\n",
       "      <td>0.160296</td>\n",
       "      <td>108</td>\n",
       "      <td>1454</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2011-01-05</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.226957</td>\n",
       "      <td>0.229270</td>\n",
       "      <td>0.436957</td>\n",
       "      <td>0.186900</td>\n",
       "      <td>82</td>\n",
       "      <td>1518</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant      dteday  season  yr  mnth  holiday  weekday  workingday  \\\n",
       "0        1  2011-01-01       1   0     1        0        6           0   \n",
       "1        2  2011-01-02       1   0     1        0        0           0   \n",
       "2        3  2011-01-03       1   0     1        0        1           1   \n",
       "3        4  2011-01-04       1   0     1        0        2           1   \n",
       "4        5  2011-01-05       1   0     1        0        3           1   \n",
       "\n",
       "   weathersit      temp     atemp       hum  windspeed  casual  registered  \\\n",
       "0           2  0.344167  0.363625  0.805833   0.160446     331         654   \n",
       "1           2  0.363478  0.353739  0.696087   0.248539     131         670   \n",
       "2           1  0.196364  0.189405  0.437273   0.248309     120        1229   \n",
       "3           1  0.200000  0.212122  0.590435   0.160296     108        1454   \n",
       "4           1  0.226957  0.229270  0.436957   0.186900      82        1518   \n",
       "\n",
       "    cnt  \n",
       "0   985  \n",
       "1   801  \n",
       "2  1349  \n",
       "3  1562  \n",
       "4  1600  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "train = pd.read_csv(\"day.csv\")\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把 dteday这一列特征进行拆分\n",
    "train['dteday']=pd.to_datetime(train['dteday'])\n",
    "train['day']=train.dteday.apply(lambda x:x.day)\n",
    "train['is_in_first_tendays'] = 0 # 上旬\n",
    "train.loc[train.day.isin(range(1,11)), 'is_in_first_tendays'] = 1\n",
    "train['is_in_middle_tendays'] = 0 # 中旬\n",
    "train.loc[train.day.isin(range(11,21)), 'is_in_middle_tendays'] = 1\n",
    "train['is_in_last_tendays'] = 0 # 下旬\n",
    "train.loc[train.day.isin(range(21,32)), 'is_in_last_tendays'] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(584, 16)\n",
      "(147, 16)\n",
      "(584,)\n",
      "(147,)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X=train.drop(['casual','registered','cnt','dteday'],axis=1)\n",
    "y=train.cnt\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=33, test_size=0.2)\n",
    "# random_state：设定随机种子，确保每次都是按照相同的方式切分数据，以保证最后的结果不会因为数据不一样的因素产生干扰。\n",
    "print(X_train.shape)\n",
    "print(X_test.shape)\n",
    "print(y_train.shape)\n",
    "print(y_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "813.4838664909103"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 线性回归\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from  sklearn.metrics import mean_squared_error# 均方误差\n",
    "lr=LinearRegression()# 实例化机器学习模型\n",
    "lr_fit=lr.fit(X_train,y_train)# 用训练数据训练模型参数\n",
    "# 用训练好的模型对测试集进行预测\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "mse=mean_squared_error(y_test,y_test_pred_lr)\n",
    "rmse=mse**(1/2)\n",
    "rmse# 用rmse作为评价指标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2914817150467951"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对比把y进行log变换之后的结果\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "log_y=np.log1p(y)\n",
    "#std = StandardScaler()\n",
    "#std_y=std.fit_transform(y.values.reshape(-1, 1))# 注意这里要reshape\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,log_y,random_state=33, test_size=0.2)\n",
    "lr_log=LinearRegression()# 实例化机器学习模型\n",
    "lr_fit=lr_log.fit(X_train,y_train)# 用训练数据训练模型参数\n",
    "y_test_pred_lr = lr_log.predict(X_test)\n",
    "mse=mean_squared_error(y_test,y_test_pred_lr)# 对测试集进行预测\n",
    "rmse=mse**(1/2)\n",
    "rmse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.06380697e-01,  1.69932832e-01,  3.94020761e+01,  3.22259150e+00,\n",
       "       -1.55192884e-01,  1.53034397e-02,  5.94892638e-02, -2.79505994e-01,\n",
       "        4.97519848e-01,  8.45137576e-01, -1.27347675e-01, -8.69402297e-01,\n",
       "        1.03673864e-01, -1.90075556e-03,  5.14959163e-02, -4.95951607e-02])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr_coef=lr_log.coef_\n",
    "lr_coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "832.2590957448667"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 岭回归,L2正则\n",
    "from sklearn.linear_model import RidgeCV\n",
    "rid=RidgeCV(alphas=[1e-3, 1e-2, 1e-1, 1])\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=33, test_size=0.2)\n",
    "rid_fit=rid.fit(X_train,y_train)\n",
    "y_train_pred_rid = rid.predict(X_train)\n",
    "y_test_pred_rid = rid.predict(X_test)\n",
    "mse=mean_squared_error(y_test,y_test_pred_rid)# 对测试集进行预测\n",
    "rmse=mse**(1/2)\n",
    "rmse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.29342038564493006"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对比把y进行log变换之后的结果\n",
    "rid_log=RidgeCV(alphas=[1e-3, 1e-2, 1e-1, 1])\n",
    "log_y=np.log1p(y)\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,log_y,random_state=33, test_size=0.2)\n",
    "rid_fit=rid_log.fit(X_train,y_train)\n",
    "y_train_pred_rid = rid_log.predict(X_train)\n",
    "y_test_pred_rid = rid_log.predict(X_test)\n",
    "mse=mean_squared_error(y_test,y_test_pred_rid)# 对测试机进行预测\n",
    "rmse=mse**(1/2)\n",
    "rmse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-5.65704450e-02,  1.65737591e-01,  2.11789027e+01,  1.70571623e+00,\n",
       "       -1.63542761e-01,  1.53559228e-02,  5.98952888e-02, -2.78334993e-01,\n",
       "        4.94042251e-01,  9.46598995e-01, -1.29813983e-01, -8.55998416e-01,\n",
       "        5.41410983e-02,  4.52815028e-04,  5.17627153e-02, -5.22155304e-02])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rid_coef=rid_log.coef_\n",
    "rid_coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1453.4244815456184"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Lasso回归，L1正则\n",
    "from sklearn.linear_model import LassoCV\n",
    "las=LassoCV(cv=5, random_state=0)\n",
    "\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=33, test_size=0.2)\n",
    "las_fit=las.fit(X_train,y_train)\n",
    "\n",
    "y_train_pred_las = las.predict(X_train)\n",
    "y_test_pred_las = las.predict(X_test)\n",
    "mse=mean_squared_error(y_test,y_test_pred_las)# 对测试集进行预测\n",
    "rmse=mse**(1/2)\n",
    "rmse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.43711271991449235"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对比把y进行log变换之后的结果\n",
    "las_log=LassoCV(cv=5, random_state=0)\n",
    "log_y=np.log1p(y)\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,log_y,random_state=33, test_size=0.2)\n",
    "las_fit=las_log.fit(X_train,y_train)\n",
    "\n",
    "y_train_pred_las = las_log.predict(X_train)\n",
    "y_test_pred_las = las_log.predict(X_test)\n",
    "mse=mean_squared_error(y_test,y_test_pred_las)# 对测试集进行预测\n",
    "rmse=mse**(1/2)\n",
    "rmse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.00131637,  0.06589737,  0.        , -0.00729754, -0.        ,\n",
       "        0.        ,  0.        , -0.15265755,  0.        ,  0.        ,\n",
       "       -0.        , -0.        , -0.00384665, -0.        ,  0.        ,\n",
       "       -0.        ])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las_coef=las_log.coef_\n",
    "las_coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>day</th>\n",
       "      <th>is_in_first_tendays</th>\n",
       "      <th>is_in_middle_tendays</th>\n",
       "      <th>is_in_last_tendays</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.344167</td>\n",
       "      <td>0.363625</td>\n",
       "      <td>0.805833</td>\n",
       "      <td>0.160446</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.363478</td>\n",
       "      <td>0.353739</td>\n",
       "      <td>0.696087</td>\n",
       "      <td>0.248539</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.196364</td>\n",
       "      <td>0.189405</td>\n",
       "      <td>0.437273</td>\n",
       "      <td>0.248309</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.212122</td>\n",
       "      <td>0.590435</td>\n",
       "      <td>0.160296</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.226957</td>\n",
       "      <td>0.229270</td>\n",
       "      <td>0.436957</td>\n",
       "      <td>0.186900</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant  season  yr  mnth  holiday  weekday  workingday  weathersit  \\\n",
       "0        1       1   0     1        0        6           0           2   \n",
       "1        2       1   0     1        0        0           0           2   \n",
       "2        3       1   0     1        0        1           1           1   \n",
       "3        4       1   0     1        0        2           1           1   \n",
       "4        5       1   0     1        0        3           1           1   \n",
       "\n",
       "       temp     atemp       hum  windspeed  day  is_in_first_tendays  \\\n",
       "0  0.344167  0.363625  0.805833   0.160446    1                    1   \n",
       "1  0.363478  0.353739  0.696087   0.248539    2                    1   \n",
       "2  0.196364  0.189405  0.437273   0.248309    3                    1   \n",
       "3  0.200000  0.212122  0.590435   0.160296    4                    1   \n",
       "4  0.226957  0.229270  0.436957   0.186900    5                    1   \n",
       "\n",
       "   is_in_middle_tendays  is_in_last_tendays  \n",
       "0                     0                   0  \n",
       "1                     0                   0  \n",
       "2                     0                   0  \n",
       "3                     0                   0  \n",
       "4                     0                   0  "
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "feat_names=['instant',  'season', 'yr', 'mnth', 'holiday', 'weekday', 'workingday',  'weathersit', 'temp', 'atemp', 'hum', 'windspeed', 'day', 'is_in_first_tendays', 'is_in_middle_tendays', 'is_in_last_tendays']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_rid</th>\n",
       "      <th>coef_las</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>instant</td>\n",
       "      <td>-0.106381</td>\n",
       "      <td>-0.056570</td>\n",
       "      <td>0.001316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season</td>\n",
       "      <td>0.169933</td>\n",
       "      <td>0.165738</td>\n",
       "      <td>0.065897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yr</td>\n",
       "      <td>39.402076</td>\n",
       "      <td>21.178903</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mnth</td>\n",
       "      <td>3.222592</td>\n",
       "      <td>1.705716</td>\n",
       "      <td>-0.007298</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>holiday</td>\n",
       "      <td>-0.155193</td>\n",
       "      <td>-0.163543</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>weekday</td>\n",
       "      <td>0.015303</td>\n",
       "      <td>0.015356</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>workingday</td>\n",
       "      <td>0.059489</td>\n",
       "      <td>0.059895</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>weathersit</td>\n",
       "      <td>-0.279506</td>\n",
       "      <td>-0.278335</td>\n",
       "      <td>-0.152658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>temp</td>\n",
       "      <td>0.497520</td>\n",
       "      <td>0.494042</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>atemp</td>\n",
       "      <td>0.845138</td>\n",
       "      <td>0.946599</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>hum</td>\n",
       "      <td>-0.127348</td>\n",
       "      <td>-0.129814</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>-0.869402</td>\n",
       "      <td>-0.855998</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>day</td>\n",
       "      <td>0.103674</td>\n",
       "      <td>0.054141</td>\n",
       "      <td>-0.003847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>is_in_first_tendays</td>\n",
       "      <td>-0.001901</td>\n",
       "      <td>0.000453</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>is_in_middle_tendays</td>\n",
       "      <td>0.051496</td>\n",
       "      <td>0.051763</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>is_in_last_tendays</td>\n",
       "      <td>-0.049595</td>\n",
       "      <td>-0.052216</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 columns    coef_lr   coef_rid  coef_las\n",
       "0                instant  -0.106381  -0.056570  0.001316\n",
       "1                 season   0.169933   0.165738  0.065897\n",
       "2                     yr  39.402076  21.178903  0.000000\n",
       "3                   mnth   3.222592   1.705716 -0.007298\n",
       "4                holiday  -0.155193  -0.163543 -0.000000\n",
       "5                weekday   0.015303   0.015356  0.000000\n",
       "6             workingday   0.059489   0.059895  0.000000\n",
       "7             weathersit  -0.279506  -0.278335 -0.152658\n",
       "8                   temp   0.497520   0.494042  0.000000\n",
       "9                  atemp   0.845138   0.946599  0.000000\n",
       "10                   hum  -0.127348  -0.129814 -0.000000\n",
       "11             windspeed  -0.869402  -0.855998 -0.000000\n",
       "12                   day   0.103674   0.054141 -0.003847\n",
       "13   is_in_first_tendays  -0.001901   0.000453 -0.000000\n",
       "14  is_in_middle_tendays   0.051496   0.051763  0.000000\n",
       "15    is_in_last_tendays  -0.049595  -0.052216 -0.000000"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fs = pd.DataFrame({\"columns\":feat_names, \"coef_lr\":lr_log.coef_,\"coef_rid\":rid_log.coef_,\"coef_las\":las_log.coef_})\n",
    "fs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 从上面的结果可以看出线性回归算法在测试集上的表现最好，岭回归和lasso回归都对参数有正则，让参数的浮动范围变小，并且lasso还会起到筛选特征的作用，上面的特征系数很多都是0，但是负面的作用就是利用的特征少了，相应的，得到的结果也不会很好。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
